import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{1,1,4,2,3};
        System.out.println(solution.minOperations(nums, 5));
    }
}
class Solution {
    public int minOperations(int[] nums, int x) {
        int ret = -1;
        int n = nums.length;
        int sum = 0;
        int tamp = 0;
        for (int i = 0;i < n;i++) {
            sum+=nums[i];
        }
        int target = sum - x;
        int ad = 0;
        if (target<0) return -1;
        for (int left = 0,right = 0;right < nums.length;right++) {
            //进窗口
            ad += nums[right];
            //出窗口
            while (left<n&&target<ad) {
                ad -= nums[left++];
            }
            if (target==ad) {
                tamp = Math.max(tamp, right - left + 1);
                ret = n - tamp;
            }
        }
        return ret;
    }
}